#!/usr/bin/env python

import sys
import os
import math

escalonador = ["FASTEST","SLOWEST"]
checkPoint = ["OFF", "ON"]
green = ["OFF"]

makespanIdle = {}

for escalona in escalonador:
    for gree in green:	
	for check in checkPoint:
		cont = 0
		for demanda in [1] + range(3000, 18001, 3000):	
			demanda = str(demanda)
			outPut1 = "results/job-makespan-"+escalona+"-"+check+"-"+gree+"-"+demanda+".out"
			outPut2 = "results/machine-state-"+escalona+"-"+check+"-"+gree+"-"+demanda+".out"
			outPut3 = "results/summary-"+escalona+"-"+check+"-"+gree+"-"+demanda+".txt"
			if os.path.isfile(outPut1):

	   	     		end = 0.0
				makespan = 0.0
				runTime = 0.0
				fileOutPut = file(outPut1, "r")
				for line in fileOutPut:			
					line.strip()
					dados = line.split("\t")
					end = float(dados[1])
					runTime = runTime+float(dados[2])
					makespan=makespan+float(dados[3])

				density = (runTime/end)
				
				makespanIdle[(escalona, gree, check, demanda)] = makespan
				cont +=1

escalonador = ["FASTEST","SLOWEST"]
checkPoint = ["OFF", "ON"]
green = ["OFF", "SLEEP", "STANDBY"]

fileAnalysisMakeSpan = file("resultsMakeSpan.txt","w")

#fileAnalysisMakeSpan.write("DEMANDA	ESCALONADOR	CHECK	GREEN	DENSIDADE	RUNTIME	MAKESPAN	INCREASE\tN_FALHAS\n")
fileAnalysisMakeSpan.write("TASK	DEMANDA	ESCALONADOR	CHECK	GREEN	DENSIDADE	RUNTIME	MAKESPAN\n")

for escalona in escalonador:
    for gree in green:	
	for check in checkPoint:
		cont = 0
		for demanda in [1] + range(3000, 18001, 3000):	
			demanda = str(demanda)
			outPut1 = "results/job-makespan-"+escalona+"-"+check+"-"+gree+"-"+demanda+".out"
			outPut2 = "results/machine-state-"+escalona+"-"+check+"-"+gree+"-"+demanda+".out"
			outPut3 = "results/summary-"+escalona+"-"+check+"-"+gree+"-"+demanda+".txt"
			if os.path.isfile(outPut1):

	   	     		end = 0.0
				makespan = 0.0
				runTime = 0.0
				falhas = 0
				contTasks=0
				fileOutPut = file(outPut1, "r")
				for line in fileOutPut:			
					line.strip()
					dados = line.split("\t")
					end = float(dados[1])
					runTime = runTime+float(dados[2])
					makespan=makespan+float(dados[3])
					falhas+=int(dados[4])
					contTasks+=1
					fileAnalysisMakeSpan.write(str(contTasks)+"\t"+str(cont)+"\t"+escalona+"\t"+check+"\t"+gree+"\t"+str(density)+"\t"+str(dados[2])+"\t"+str(dados[3])+"\n")

				density = (runTime/end)
				falhas = contTasks-falhas
				makespan_off = makespanIdle[(escalona, "OFF", check, demanda)]
				increaseProportion = 100.0 * (makespan - makespan_off) / makespan_off

				cont +=1

				#fileAnalysisMakeSpan.write(str(cont)+"\t"+escalona+"\t"+check+"\t"+gree+"\t"+str(density)+"\t"+str(runTime)+"\t"+str(makespan/contTasks)+"\t"+str(increaseProportion)+"\t"+str(falhas)+"\n")

